package ch.lom.clemens.android.bibliography;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import ch.lom.clemens.android.bibliography.data.MCollection;


public class CollectionsActivity extends ListActivity {
	private static ArrayList<HashMap<String,Object>> m_list = new ArrayList<HashMap<String,Object>>();	
	private SimpleAdapter m_adapter;
	
	public static CollectionsActivity singleton;

    public void retrieveCollections() {

		m_list.clear();
		
		HashMap<String,Object> libItem = new HashMap<String,Object>();
		libItem.put("line1", "All documents");
		libItem.put("line2", "");
		libItem.put("icon", R.drawable.collection);
		libItem.put("MCollection",new MCollection(StartActivity.m_mendeley,MCollection.LIBRARY));
		m_list.add(libItem);
		
		HashMap<String,Object> autItem = new HashMap<String,Object>();
		autItem.put("line1", "My publications");
		autItem.put("line2", "");
		autItem.put("icon", R.drawable.collection);
		autItem.put("MCollection",new MCollection(StartActivity.m_mendeley,MCollection.AUTHORED));
		m_list.add(autItem);
		
		MCollection[] collections = StartActivity.m_mendeley.getCollections();
		
		if (collections != null) {
		
			for (int i=0; i<collections.length; i++) {
				HashMap<String,Object> item = new HashMap<String,Object>();
	
				item.put("line1", collections[i].name);
				item.put("line2", collections[i].size + " documents");
				item.put("icon", R.drawable.collection);
				item.put("MCollection", collections[i]);
	
				// the JSON object members
				
				m_list.add(item);
			}
		}
		
		MCollection[] groups = StartActivity.m_mendeley.getGroups(StartActivity.m_mendeley);
		if (groups != null) {
			
			for (int i=0; i<groups.length; i++) {
				HashMap<String,Object> item = new HashMap<String,Object>();
	
				item.put("line1", groups[i].name);
				item.put("line2", groups[i].size + " documents");
				item.put("icon", R.drawable.group);
				item.put("MCollection", groups[i]);
	
				m_list.add(item);
			}
		}
		
		m_adapter.notifyDataSetChanged();
    }
    
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        singleton = this;

        if (m_list.isEmpty()) {
	        HashMap<String,Object> item = new HashMap<String,Object>();
	        item.put("line1", "Accessing data");
	        item.put("line2", "Please wait...");
	        item.put("icon", R.drawable.collection);
	        m_list.add(item);
	        
        }
        
		m_adapter = new SimpleAdapter(this, m_list, R.layout.collectionsitem, new String[]{"line1","line2","icon"}, new int[]{R.id.bt_text1, R.id.bt_text2, R.id.bt_icon});
		setListAdapter(m_adapter);

		retrieveCollections();
    }
    
    protected void onListItemClick(ListView lv, View v, int position, long id) {
		@SuppressWarnings("unchecked")
		HashMap<String,Object> item = (HashMap<String, Object>)m_adapter.getItem(position);
		
		MCollection col = (MCollection)item.get("MCollection");
		String strName = (String)item.get("line1");
		
		if (col != null) {

			DocumentsActivity.m_newCol   = col;
			
			//TODO: check whether this is a group or a collection
			StartActivity.singleton.getTabHost().setCurrentTabByTag(StartActivity.TAB_DOCUMENTS);
			StartActivity.singleton.setTitle(strName);
		}
		
	}
    
    public void onResume() {
    	super.onResume();
    	getParent().setTitle( getString(R.string.app_name) );
    }
    
}
